﻿using Abp.Domain.Repositories;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading;

namespace Mes5GService.MES
{
    public class ImportTask : IImportTask
    {
        private readonly IRepository<BopTree, Guid> _bopTreeRepository;
        public ImportTask(IRepository<BopTree, Guid> bopTreeRepository)
        {
            this._bopTreeRepository = bopTreeRepository;
        }
        public void Import(string path)
        {
            string target = ExtractZip(path);
            //读取bop文件
            string bopPath = Path.Combine(target,"Bop.json");
            FileInfo fileInfo = new FileInfo(bopPath);
            if (!fileInfo.Exists)
            {
                //报错
                return;
            }
            var bopTree = BopTreeConverter.ReadJson(bopPath);
            _bopTreeRepository.Insert(bopTree);
            return;
        }
        private string ExtractZip(string path)
        {
            string dirName = Path.GetFileNameWithoutExtension(path);
            string target = Path.Combine(Path.GetDirectoryName(path),dirName);
            ZipFile.ExtractToDirectory(path, target, Encoding.GetEncoding("GB2312"));
            return target;
        }
    }
}
